package com.library.service.serviceImpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.library.dto.pojo.excelPo.ReaderExportInfo;
import com.library.dto.pojo.excelPo.ReaderImportInfo;
import com.library.dto.vo.ReaderVo;
import com.library.entity.Reader;
import com.library.entity.ReaderType;
import com.library.mapper.ReaderMapper;
import com.library.mapper.ReaderTypeMapper;
import com.library.service.IReaderService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 至尊伍人组
 * @since 2018-09-30
 */
@Service
public class ReaderServiceImpl extends ServiceImpl<ReaderMapper, Reader> implements IReaderService {

    @Autowired
    ReaderTypeMapper readerTypeMapper;

    /**
     * 根据条件，返回ReadVo封装类的的集合
     *
     * @param wrapper
     * @return
     */
    public List<ReaderVo> selectListAsReaderVo( EntityWrapper<Reader> wrapper ) {

        List<ReaderVo> readerVos = new ArrayList<>();

        Map<Integer, ReaderType> readTypeMap = getReadTypeMap();

        List<Reader> readers = super.selectList(wrapper);
        readers.parallelStream()
            .forEach(e -> {
                try {
                    ReaderVo readerVo = new ReaderVo();
                    ReaderType readerType = readTypeMap.get(e.getReaderTypeId());
                    BeanUtils.copyProperties(readerVo, readerType);
                    BeanUtils.copyProperties(readerVo, e);

                    readerVos.add(readerVo);
                } catch ( Exception e1 ) {
                }
            });
        return readerVos;
    }

    @Override
    public List<ReaderExportInfo> getReaderExcelList() {

        //获得所有的读者的种类
        Map<Integer, ReaderType> readTypeMap = getReadTypeMap();

        //获得数据库中所有的读者的信息
        List<Reader> readers = super.selectList(null);

        //要返回的封装后的导出对象集合
        ArrayList<ReaderExportInfo> readerExportInfos = new ArrayList<>();

        readers.parallelStream()
            .forEach(e -> {
                try {
                    //一条excel记录对应的对象
                    ReaderExportInfo readerExportInfo = new ReaderExportInfo();
                    ReaderType readerType = readTypeMap.get(e.getReaderTypeId());
                    BeanUtils.copyProperties(readerExportInfo, readerType);
                    BeanUtils.copyProperties(readerExportInfo, e);
                    readerExportInfos.add(readerExportInfo);
                } catch ( Exception e1 ) {
                }
            });
        return readerExportInfos;
    }

    /**
     * 将excel表格中的记录转换为pojo，同时插入到数据库中
     *
     * @param objects
     */
    @Override
    public void insertBatchFromExcel( List<Object> objects ) {

        List<Reader> readers = new ArrayList<>();
        //当前时间对象
        Date date = new Date();

        //进行类型转换，同时加入到目标集合对象中
        objects.parallelStream()
            .forEach(e -> {
                try {
                    Reader reader = new Reader();
                    ReaderImportInfo importInfo = (ReaderImportInfo)e;
                    BeanUtils.copyProperties(reader, importInfo);
                    reader.setCreateTime(date);
                    readers.add(reader);
                } catch ( Exception e1 ) {
                }
            });

        //批量插入到数据库中
        super.insertBatch(readers);

    }

    /**
     * 返回所有读者类型的map集合
     *
     * @return
     */
    public Map<Integer, ReaderType> getReadTypeMap() {
        List<ReaderType> typeList = readerTypeMapper.selectList(null);

        Map<Integer, ReaderType> readerTypeMap = null;
        readerTypeMap = typeList
                            .parallelStream()
                            .collect(Collectors
                                         .toMap(ReaderType::getReaderId,
                                             readerType -> readerType)
                            );

        return readerTypeMap;
    }

}
